package top.xierongsheng.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import top.xierongsheng.code.BaseVo;
import top.xierongsheng.code.R;
import top.xierongsheng.code.ResultVo;
import top.xierongsheng.user.model.SysArea;
import top.xierongsheng.user.model.SysUser;
import top.xierongsheng.user.service.IAreaService;
import top.xierongsheng.user.service.IUserService;

import java.util.List;

@Slf4j
@RestController //这意味着这个类是一个控制器
@RequestMapping(path = "/area") //这意味着URL以/resource开头（在应用程序路径之后）
@CrossOrigin
@RequiredArgsConstructor
@Tag(name = "地区接口")
public class AreaController {

    private final IAreaService areaService;

    private final IUserService userService;

    /**
     * 根据地区id获取地区列表
     *
     * @param userId 用户Id
     * @param areaId 地区Id
     */
    @GetMapping(path = "getAreaListByAreaId")
    @Operation(summary = "根据地区id获取地区列表")
    public List<SysArea> getAreaListByAreaId(@RequestHeader(name = "userId") String userId, @RequestParam Integer areaId) {
        return areaService.list(new QueryWrapper<SysArea>().lambda().eq(SysArea::getAreaParentId, areaId));
    }

    /**
     * 根据地区id获取地区列表
     *
     * @param userId
     * @param areaId
     * @return
     */
    @GetMapping(path = "getAreaListByAreaId2")
    @Operation(summary = "根据地区id获取地区列表2")
    public R getAreaListByAreaId2(@RequestHeader(name = "userId") String userId, @RequestParam Integer areaId) {
        List<SysArea> sysAreaList = areaService.list(new QueryWrapper<SysArea>().lambda().eq(SysArea::getAreaParentId, areaId));
        return R.ok().zip("data", sysAreaList);

    }

    /**
     * 根据地区id获取地区列表
     *
     * @param userId
     * @param areaId
     * @return
     */
    @GetMapping(path = "getAreaListByAreaId3")
    @Operation(summary = "根据地区id获取地区列表3")
    public ResultVo getAreaListByAreaId3(@RequestHeader(name = "userId") String userId, @RequestParam Integer areaId) {
        List<SysArea> sysAreaList = areaService.list(new QueryWrapper<SysArea>().lambda().eq(SysArea::getAreaParentId, areaId));
        return ResultVo.ok().data(sysAreaList);
    }

    /**
     * 根据地区id获取地区列表
     *
     * @param userId
     * @param areaId
     * @return
     */
    @GetMapping(path = "getAreaInfoByAreaId")
    @Operation(summary = "根据地区id获取地区信息")
    public BaseVo<SysArea> getAreaInfoByAreaId(@RequestHeader(name = "userId") String userId, @RequestParam Integer areaId) {
        SysArea sysArea = areaService.getById(areaId);
        return BaseVo.ok(sysArea);
    }

    /**
     * 根据地区id获取地区列表
     *
     * @param userId
     * @param id
     * @return
     */
    @GetMapping(path = "getUserInfoById")
    @Operation(summary = "根据用户id获取用户信息")
    public BaseVo<SysUser> getUserInfoById(@RequestHeader(name = "userId") String userId, @RequestParam Integer id) {
        SysUser sysArea = userService.getById(id);
        return BaseVo.ok(sysArea);
    }
}
